package com.czl.module_work.viewModel

import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.GuestApplyParam
import com.czl.base.data.bean.ImgRspBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.czl.module_work.util.PhoneFormatCheckUtils

class UserInviteViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    val uc = UiChangeEvent()

    val startTime = ObservableField<String>()
    val endTime = ObservableField<String>()

    //访客姓名
    val guestName = ObservableField<String>()

    //来访目的
    val guestPurpose = ObservableField<String>()

    //
    val guestFaceUrl = ObservableField<String>()

    //访客电话
    val guestMobile = ObservableField<String>()

    //身份证
    val identity = ObservableField<String>()

    //被访人电话
    val visitMobile = ObservableField<String>()

    //被访人姓名
    val visitName = ObservableField<String>()

    //楼栋
    val buildName = ObservableField<String>()

    //单元
    val unitName = ObservableField<String>()

    val typeName = ObservableField<String>("选择证件类型")
    val inviteTypeName = ObservableField<String>()
    val typeCode = ObservableField<String>()

    init {
        visitName.set(model.getUserData()?.loginUser?.name ?: "")
        visitMobile.set(model.getUserData()?.loginUser?.phone ?: "")
    }


    class UiChangeEvent {
        val showTime: SingleLiveEvent<String> = SingleLiveEvent()
        val choiceImgEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val deleteImgEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val choiceTypeEvent: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val showChoiceInviteTypeClick = BindingCommand<Void>(BindingAction {
        //选择访客类型
        uc.choiceTypeEvent.postValue("0")
    })

    val showChoiceTypeClick = BindingCommand<Void>(BindingAction {
        //选择证件类型
        uc.choiceTypeEvent.postValue("1")
    })
    val showChoiceImgClick = BindingCommand<Void>(BindingAction {
        uc.choiceImgEvent.call()
    })
    val deleteImgClick = BindingCommand<Void>(BindingAction {
        uc.deleteImgEvent.call()
    })
    val showStartTimeClick = BindingCommand<Void>(BindingAction {
        uc.showTime.postValue("0")
    })

    val showStopTimeClick = BindingCommand<Void>(BindingAction {
        uc.showTime.postValue("1")
    })

    val btnMenu1Click: BindingCommand<Void> = BindingCommand(BindingAction {
//        startContainerActivity(AppConstants.Router.Work.FWORK)
    })


    val btnMenu2Click: BindingCommand<Void> = BindingCommand(BindingAction {
        addApply()
    })

    fun addApply() {
        if(TextUtils.isEmpty(inviteTypeName.get())){
            ToastUtils.showShort("访客类型不能为空")
            return
        }
        if (TextUtils.isEmpty(startTime.get())) {
            ToastUtils.showShort("来访时间不能为空")
            return
        }
        if (TextUtils.isEmpty(endTime.get())) {
            ToastUtils.showShort("结束时间不能为空")
            return
        }
        if (TextUtils.isEmpty(guestName.get())) {
            ToastUtils.showShort("访客姓名不能为空")
            return
        }
        if (TextUtils.isEmpty(guestMobile.get())) {
            ToastUtils.showShort("访客电话不能为空")
            return
        }
//        if (TextUtils.isEmpty(identity.get())) {
//            ToastUtils.showShort("访客身份证不能为空")
//            return
//        }
        if (TextUtils.isEmpty(visitName.get())) {
            ToastUtils.showShort("被访人姓名不能为空")
            return
        }
        if (TextUtils.isEmpty(visitMobile.get())) {
            ToastUtils.showShort("被访人电话不能为空")
            return
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(guestMobile.get() ?: "")) {
            ToastUtils.showShort("请输入正确的手机号")
            return
        }
        when(typeName.get()){
            "一代身份证" ->{
                if(typeCode.get()!!.length != 15){
                    ToastUtils.showShort("请输入正确的一代身份证")
                    return
                }
            }
            "二代身份证" ->{
                if(typeCode.get()!!.length != 18){
                    ToastUtils.showShort("请输入正确的二代身份证")
                    return
                }
            }
            "驾驶证" ->{
                if(typeCode.get()!!.length != 12){
                    ToastUtils.showShort("请输入正确的驾驶证")
                    return
                }
            }
            "军官证" ->{
                if(typeCode.get()!!.length != 7){
                    ToastUtils.showShort("请输入正确的军官证")
                    return
                }
            }
            "居住证" ->{
                if(typeCode.get()!!.length != 18){
                    ToastUtils.showShort("请输入正确的居住证")
                    return
                }
            }
            "护照" ->{
                if(typeCode.get()!!.length != 9){
                    ToastUtils.showShort("请输入正确的护照")
                    return
                }
            }
            "回乡证" ->{
                if(typeCode.get()!!.length != 10){
                    ToastUtils.showShort("请输入正确的回乡证")
                    return
                }
            }
            "港澳通行证" ->{
                if(typeCode.get()!!.length != 9){
                    ToastUtils.showShort("请输入正确的港澳通行证")
                    return
                }
            }
            "台胞证" ->{
                if(typeCode.get()!!.length != 8){
                    ToastUtils.showShort("请输入正确的台胞证")
                    return
                }
            }
        }
//        if ((identity.get() ?: "").length < 18) {
//            ToastUtils.showShort("身份证号不能小于18位")
//            return
//        }
        val params = GuestApplyParam(
            guestType = 2,
            guestUserType = inviteTypeName.get()?:"",
            identity = typeCode.get()?:"",
            guestCardType = typeName.get()?:"",
            startDateTime = startTime.get() ?: "",
            endDateTime = endTime.get() ?: "",
            guestName = guestName.get() ?: "",
            guestMobile = guestMobile.get() ?: "",
            guestVehicleNo = identity.get() ?: "",
            guestPurpose = guestPurpose.get() ?: "",
            guestFaceUrl = guestFaceUrl.get() ?: "",
            visitName = visitName.get() ?: "",
            visitMobile = visitMobile.get() ?: "",
            unitName = unitName.get() ?: "",
            buildName = buildName.get() ?: "",
            areaId = model.getAreaId()
        )
        model.addApply(params)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<String>>() {
                override fun onResult(t: BaseBean<String>) {
                    if (t.code == 200) {
                        startContainerActivity(
                            AppConstants.Router.Work.F_WORK_MY_INVITE,
                            Bundle().apply {
                                putString(AppConstants.BundleKey.WORK_MY_INVITE_ID, t.data ?: "")
                            })
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }
            })
    }

    fun uploadImg(imgSrc: String) {
        model.apply {
            uploadImg(imgSrc)
                .compose(RxThreadHelper.rxSchedulerHelper(this@UserInviteViewModel))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<ImgRspBean>>() {
                    override fun onResult(t: BaseBean<ImgRspBean>) {
                        dismissLoading()
                        if (t.code == 200) {
                            guestFaceUrl.set(t.data?.url)
                        } else {
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showErrorToast(msg)
                    }

                })
        }
    }
}